package com.dataseek.iot.analysis.manager.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.analysis.manager.vo.DeviceInfoVo;
import com.dataseek.iot.device.manager.vo.DeviceDetailVo;
import com.dataseek.iot.mysql.entity.*;
import com.dataseek.iot.mysql.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author cc
 * @Title: 数据服务handler
 * @Description: 数据服务handler
 * @date 2021/10/1011:12 上午
 * @since 1.0
 */
@Service
@Slf4j
public class DataService {

    @Autowired
    ProductServiceImpl productService;

    @Autowired
    DeviceServiceImpl deviceService;

    @Autowired
    LineServiceImpl lineService;

    @Autowired
    AreaServiceImpl areaService;

    @Autowired
    LocationServiceImpl locationService;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    RoleServiceImpl roleService;

    /**
     * 设备详情
     * @param deviceId
     * @return
     */
    public DeviceDetailVo deviceDetails(String deviceId){

        assert Objects.nonNull(deviceId);

        DeviceDetailVo deviceDetailVo  = new DeviceDetailVo();

        Product product  = null;

        Line line = null;

        District district = null;


        Device device = deviceService.getById(deviceId);

        if(Objects.nonNull(device)){
            deviceDetailVo.setDeviceCode(device.getDeviceCode());
            deviceDetailVo.setDeviceName(device.getDeviceName());
            deviceDetailVo.setDeviceLocation(device.getLocation());
            deviceDetailVo.setDeviceComment(device.getComment());
            product = productService.getById(device.getProductId());
        }

        if(Objects.nonNull(product)){
            deviceDetailVo.setProductCode(product.getProductCode());
            deviceDetailVo.setProductName(product.getProductName());
            deviceDetailVo.setProductTag(product.getTag());
            line = lineService.getOneByCode(device.getLineCode());
        }

        if(Objects.nonNull(line)){
            deviceDetailVo.setLineCode(line.getLineCode());
            deviceDetailVo.setLineName(line.getLineName());
        }

        if(Objects.nonNull(district)){
            deviceDetailVo.setDistrictCode(district.getDistrictCode());
            deviceDetailVo.setDeviceName(district.getDistrictName());
        }


        return deviceDetailVo;
    }


    /**
     * 获取所有的设备列表
     * @param username
     * @return
     */
    public List<Device> getAllDevices(String username){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        User user = userService.getOne(wrapper);
        String lineCode = user.getLineCode();
        String[] codes = lineCode.split(",");
        List<Device> devices = new ArrayList<>();
        for (String c : codes) {
            List<Line> lines = lineService.getLinesByCode(c);
            for (Line l : lines) {
                String currentLineCode = l.getLineCode();
                wrapper = new QueryWrapper<>();
                wrapper.eq("line_code", currentLineCode);
                List<Device> result = deviceService.list(wrapper);
                devices.addAll(result);
            }
        }
        return devices;
    }



    public List<DeviceInfoVo> getAllDeviceInfo(String username){
        QueryWrapper wrapper = new QueryWrapper();

        Area area = null;
        Location location = null;
        Line line;
        List<DeviceInfoVo> list = new ArrayList<>();

        wrapper.eq("username", username);
        User user = userService.getOne(wrapper);
        if(Objects.isNull(user)){
            log.error("无此用户");

        }
        String roleCode = roleService.getById(user.getRoleId()).getRoleCode();
        //如果是超级用户
        if(roleCode.equals("superAdmin")){
            List<Device> devices =  deviceService.list();
            for(Device d : devices){
                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                String[] codes  = d.getLineCode().split("-");
                if(codes.length<2){
                    log.error("线路编码不符合要求");
                    continue;
                }
                String areaCode = codes[2];
                area = areaService.getOneByCode(areaCode);
                line = lineService.getOneByCode(d.getLineCode());

                if (Objects.nonNull(area)) {
                    location = locationService.getLocationByCode(area.getAreaId());
                }

                list.add(createDeviceInfoVo(d, line, area, location));
            }
            return list;
        }

        String lineCode = user.getLineCode();
        String[] codes = lineCode.split(",");

        for (String c : codes) {
            List<Line> lines = lineService.getLinesByCode(c);
            for (Line l : lines) {
                if (Objects.nonNull(l)) {
                    area = areaService.getOneByCode(l.getAreaCode());
                }
                if (Objects.nonNull(area)) {
                    location = locationService.getLocationByCode(area.getAreaId());
                }
                String currentLineCode = l.getLineCode();
                wrapper = new QueryWrapper<>();
                wrapper.eq("line_code", currentLineCode);
                List<Device> devices = deviceService.list(wrapper);
                for (Device d : devices) {
                    list.add(createDeviceInfoVo(d, l, area, location));
                }
            }
        }
        return list;
    }

    /**
     * 创建设备信息vo
     * @param d
     * @return
     */
    private DeviceInfoVo createDeviceInfoVo(Device d, Line l, Area area,Location location){
        DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
        if (Objects.nonNull(d)) {
            deviceInfoVo.setDeviceCode(d.getDeviceCode());
            deviceInfoVo.setDeviceLocation(d.getLocation());
            deviceInfoVo.setDeviceName(d.getDeviceName());
            deviceInfoVo.setStatus(d.getState());
        }
        //设置区域信息
        if (Objects.nonNull(area)) {
            deviceInfoVo.setAreaCode(area.getAreaId());
            deviceInfoVo.setAreaName(area.getArea());
        }
        //设置经纬度信息
        if (Objects.nonNull(location)) {
            String longitude = location.getLongitude();
            String latitude = location.getLatitude();
            deviceInfoVo.setAreaLocation(longitude + "," + latitude);
        }
        //设置线路信息
        if (Objects.nonNull(l)) {
            deviceInfoVo.setLineCode(l.getLineCode());
            deviceInfoVo.setLineName(l.getLineName());
        }
        return deviceInfoVo;
    }


}
